package com.jn.okhttp.views;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.jn.okhttp.R;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private EditText editText;
    private Button buttonGet, buttonPost, buttonDict, buttonTest, buttonImageList; // 新增按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.edit);
        buttonGet = findViewById(R.id.button_get);
        buttonPost = findViewById(R.id.button_post);
        buttonDict = findViewById(R.id.button_dict);
        buttonImageList = findViewById(R.id.button_image_list); // 新增的显示图片列表按钮

        // GET 按钮点击事件
        buttonGet.setOnClickListener(view -> {
            String platform = editText.getText().toString();
            String url = generateUrl(platform); // 生成 URL
            if (url != null) {
                fetchData(url); // 获取数据
            } else {
                Log.e(TAG, "无效的平台类型");
            }
        });

        // 新增的按钮点击事件，跳转到图片列表
        buttonImageList.setOnClickListener(view -> {
            String apiUrl = "https://api.oioweb.cn/api/bing"; // 固定使用 Bing API
            Intent intent = new Intent(MainActivity.this, ImageListActivity.class);
            intent.putExtra("api_url", apiUrl); // 传递 API URL
            startActivity(intent);
        });

        // POST 按钮点击事件
        buttonPost.setOnClickListener(view -> {
            String url = editText.getText().toString();
            Intent intent = new Intent(MainActivity.this, TranslationActivity.class);
            intent.putExtra("url", url);
            startActivity(intent);
        });

        // 字典按钮点击事件
        buttonDict.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, DataDisplayActivity.class);
            intent.putExtra("url", "https://api.oioweb.cn/api/txt/dict");
            startActivity(intent);
        });
    }

    private String generateUrl(String platform) {
        switch (platform) {
            case "baidu":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=baidu";
            case "zhihu":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=zhihu";
            case "weixin":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=weixin";
            case "bilibili":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=bilibili";
            case "douyin":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=douyin";
            case "news":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=news";
            case "lishi":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=lishi";
            case "toutiao":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=toutiao";
            case "weibo":
                return "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=weibo";
            default:
                return null; // 输入无效
        }
    }

    private void fetchData(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "请求失败: ", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseData = response.body().string();
                    // 处理数据，例如可以传给下一个页面
                } else {
                    Log.e(TAG, "请求失败: " + response.message());
                }
            }
        });
    }
}
